package com.youaishujuhui.devops.web.controller.manage;

import javax.servlet.http.HttpServletRequest;

import org.nutz.dao.Dao;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.impl.SimpleDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.youaishujuhui.devops.web.util.MigrationTool;
import com.youaishujuhui.devops.web.vo.Result;

@Controller
@RequestMapping("/manage/system")
public class SysController {

    @RequestMapping("/page")
    public ModelAndView page(HttpServletRequest request) {
        return new ModelAndView("manage/system");
    }
	
    @RequestMapping("/sql_migration")
    @ResponseBody
    public Result<String> sqlMigration(HttpServletRequest request) {
    	Result<String> result = new Result<>();
    	SimpleDataSource dataSource = null;
    	SimpleDataSource dataSource1 = null;
    	
    	try {
    		dataSource = new SimpleDataSource();
    		dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1/study_mysql_test?useSSL=false&characterEncoding=UTF-8");
    		dataSource.setUsername("root");
    		dataSource.setPassword("qweasdzxc");

    		Dao dao = new NutDao(dataSource);
    		
    		// 创建一个数据源
    		dataSource1 = new SimpleDataSource();
    		dataSource1.setJdbcUrl("jdbc:mysql://127.0.0.1/devops_web?useSSL=false&characterEncoding=UTF-8");
    		dataSource1.setUsername("root");
    		dataSource1.setPassword("qweasdzxc");

    		// 创建一个NutDao实例,在真实项目中, NutDao通常由ioc托管, 使用注入的方式获得.
    		Dao dao1 = new NutDao(dataSource1);
    		
    		MigrationTool.migrationGame(dao, dao1);
    		MigrationTool.migrationHost(dao, dao1);
    		MigrationTool.migrationChannel(dao, dao1);
    		MigrationTool.migrationGameProcess(dao, dao1);
    		
    		result.setCode(Result.SUCCESS);
            result.setMsg("迁移成功");
            
    	} catch (Exception e) {
    		e.printStackTrace();
    		result.setCode(Result.FAIL);
            result.setMsg(e.getMessage());
    	} finally {
    		if (dataSource != null) {
    			dataSource.close();
    			dataSource = null;
    		}
    		
    		if (dataSource1 != null) {
    			dataSource1.close();
    			dataSource1 = null;
    		}
    	}

        return result;
    }
      
}
